package package1;

import java.awt.Color;

import package1.ParserExpressoes;


public class AlgoritmoPviEuler {

	Plotter2d plotter;
	Function function;
	
	public AlgoritmoPviEuler() {

		this.plotter = new Plotter2d(640,250);
		
		// Plotter configuration.
		this.plotter.setRange(-2, 13, -2, 4);
		this.plotter.setAxisColor(Color.green);
		this.plotter.setTicks(0.5, 0.5, 5);
		this.plotter.setTicksSize(10);
		this.plotter.setTicksColor(Color.white);
		this.plotter.setLabels(1.0, 0.5);
		this.plotter.setLabelColor(Color.green);
		this.plotter.setFreeAxis(true);
		this.plotter.setBackground(Color.darkGray);
		// End plotter configuration.

		// Function 1 setup.
		this.function = plotter.addFunction();
		this.function.setAreaColor(Color.yellow, 0.4);
		this.function.setShowArea(false);
		this.function.setFillArea(false);
		this.function.setAreaStep(5);
		this.function.setFillPoint(true);
		this.function.setLineColor(Color.yellow);
		this.function.setShowLine(true);
		this.function.setPointsColor(Color.yellow);
		this.function.setPointsSize(1);
		
/*		try {
			algoritmo.calculaPassos("x",0,1,0.1,400);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	}
	
	
	public Plotter2d getPlotter() {
		return this.plotter;
	}

	public void calculaPassos(String strFunction, double x0, double y0, double h, int n) throws Exception {

		Parser parser = new Parser(strFunction);
		double x, y;
		
		if (n <= 0) {
			System.out.println("Intervalo invalido.");
		} else {
			y = y0;
			x = x0;
			
			Gui.dados[0][0] = 0;
			Gui.dados[0][1] = x;
			Gui.dados[0][2] = y;
			Gui.dados[0][3] = parser.eval(x);
			
			for(int i = 1; i <= n; i++){
				this.function.addPoint(x, y);
				y = y + h*parser.eval(x);
				x = x + h;
				
				Gui.dados[i][0] = i;
				Gui.dados[i][1] = x;
				Gui.dados[i][2] = y;
				Gui.dados[i][3] = parser.eval(x);
				
				
			}
		}

	}

	public static class Parser {
		private final String str;
		ParserExpressoes parser;
		
		public Parser(final String str) {
			this.str = str;
			this.parser = new ParserExpressoes();
		}
		
		public double eval(double x) throws Exception {
			return parser.getValorFuncao(str, x);
		}
	}
}
